﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using GhostStory.Model;

namespace GhostStory.Controller
{
    class MasterController
    {
        Model.Model m_model;
        View.View m_view;
        View.Content m_content;
        View.Input m_input;
        private View.Camera m_camera = new View.Camera();
        private bool m_checkIfFirst = false;

        public enum TestState
        {
            StatePlaying,
            StateDead,
            StateClear,
            StateStart
        };

        private TestState m_state = TestState.StateStart;

        

        public MasterController(GraphicsDeviceManager a_manager, ContentManager a_contentManager)
        {
            m_model = new Model.Model();
            m_content = new View.Content(a_contentManager);
            m_input = new View.Input(m_content);
            m_view = new View.View(a_manager, m_content);
            
        }

        internal void Update(float a_elapsedTime)
        {
            bool noPush = true;
            m_view.playBgMusic();
            if (m_model.isDead())
                m_state = TestState.StateDead;
            if (m_model.isClear())
            {
                if (!m_checkIfFirst)
                {
                    m_input.DoWarp();
                    m_checkIfFirst = true;
                }
                    
                m_state = TestState.StateClear;
            }
            if (m_state == TestState.StatePlaying)
            {
                m_checkIfFirst = false;
                if (m_input.DidPlayerJump())
                {
                    noPush = false;
                    if (m_model.CanJump())
                    {
                        m_input.DoJump();
                        m_model.DoJump();
                    }
                }
                if (m_input.DidPlayerWalkRight())
                {
                    noPush = false;
                    if (m_model.CanWalkRight())
                    {
                        m_input.DoWalkRight();
                        m_model.WalkRight();
                    }
                }
                if (m_input.DidPlayerWalkLeft())
                {
                    noPush = false;
                    if (m_model.CanWalkLeft())
                    {
                        m_input.DoWalkLeft();
                        m_model.WalkLeft();
                    }
                }
                if (m_model.DidPlayerTakeCoin())
                {
                    m_input.DidPlayerTakeCoin();
                }
                if (noPush)
                {
                    m_model.Stop();
                }

                bool died = false;
                m_model.whoKilledWho(ref died);
                if (died)
                    m_state = TestState.StateDead;

            }
            else if (m_state == TestState.StateStart)
            {
                
                if (m_input.DidPlayerPressEnter())
                {
                    m_input.setLevel(m_model.getCurrentLevel());
                    m_model.RestartGame();
                    m_state = TestState.StatePlaying;
                }
            }

            else if (m_state == TestState.StateDead)
            {
                if (m_input.DidPlayerPressEnter())
                {
                    m_model.setFirstLevel();
                    m_input.setLevel(m_model.getCurrentLevel());
                    m_model.RestartGame();
                    m_model.resetPoints();
                    m_state = TestState.StatePlaying;

                }
            }

            else if (m_state == TestState.StateClear)
            {
                if (m_input.DidPlayerPressEnter())
                {
                    
                    m_model.setNextLevel();
                    m_input.setLevel(m_model.getCurrentLevel());
                    m_model.RestartGame();

                    m_state = TestState.StatePlaying;

                }
                if (m_input.DidPlayerRestart())
                {
                    m_model.setFirstLevel();
                    m_input.setLevel(m_model.getCurrentLevel());
                    m_model.RestartGame();
                    m_model.resetPoints();
                    m_state = TestState.StatePlaying;
                }
            }

            m_model.Update(a_elapsedTime, m_view);
        }

        internal void Draw(float a_elapsedTime, Microsoft.Xna.Framework.Graphics.GraphicsDevice GraphicsDevice)
        {
            //get the player position for the view
            Model.Level level = m_model.GetLevel();

            //update camera
            m_camera.CenterOn(m_model.GetPlayerPosition(),
                              new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height),
                              new Vector2(Model.Level.g_levelWidth, Model.Level.g_levelHeight));

            m_camera.SetZoom(100);

            //draw background and player 
            m_view.DrawLevel(GraphicsDevice, level, m_camera, m_model.GetPlayerPosition(), m_model.getPlayerPoints(), a_elapsedTime, m_state);

            foreach (Enemy enemy in m_model.getEnemies())
                m_view.DrawEnemyAt(GraphicsDevice, m_camera, enemy.getPosition()); 

            if (m_state == Controller.MasterController.TestState.StateDead)
            {
                if (m_model.getCurrentLevel() == Level.CurrentLevel.LEVEL_1)
                    m_view.DrawMenu(GraphicsDevice, m_camera, m_model.GetPlayerPosition(), m_model.getPlayerPoints(), Color.Yellow);
                if (m_model.getCurrentLevel() == Level.CurrentLevel.LEVEL_2)
                    m_view.DrawMenu(GraphicsDevice, m_camera, m_model.GetPlayerPosition(), m_model.getPlayerPoints(), Color.Black);
                if (m_model.getCurrentLevel() == Level.CurrentLevel.LEVEL_3)
                    m_view.DrawMenu(GraphicsDevice, m_camera, m_model.GetPlayerPosition(), m_model.getPlayerPoints(), Color.Black);
            }
            if (m_state == Controller.MasterController.TestState.StateClear)
            {
                if(m_model.getCurrentLevel() == Level.CurrentLevel.LEVEL_1)
                    m_view.DrawClearLevel1(GraphicsDevice, m_camera, m_model.GetPlayerPosition(), m_model.getPlayerPoints());
                if (m_model.getCurrentLevel() == Level.CurrentLevel.LEVEL_2)
                    m_view.DrawClearLevel2(GraphicsDevice, m_camera, m_model.GetPlayerPosition(), m_model.getPlayerPoints());
                if (m_model.getCurrentLevel() == Level.CurrentLevel.LEVEL_3)
                    m_view.DrawClearLevel3(GraphicsDevice, m_camera, m_model.GetPlayerPosition(), m_model.getPlayerPoints());
            }
            if (m_state == Controller.MasterController.TestState.StateStart)
            {
                m_view.DrawStart(GraphicsDevice, m_camera, m_model.GetPlayerPosition(), m_model.getPlayerPoints());
            }
        }

    }
}
